package com.wuyan.locationdog.ui.my

import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingFragment
import com.arcsoft.commonbase.ext.createEmptyView
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.DeviceShareListAdapter
import com.wuyan.locationdog.databinding.MyOrderFragmentBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.finishRefreshAndLoadMoreNoMoreData
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MyShareListFragment(var title: String) : BaseBindingFragment<MyOrderFragmentBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    var fragments = mutableListOf<Fragment>()

    private val mAdapter by lazy { DeviceShareListAdapter(R.layout.item_device_share_list, null) }

    override fun lazyLoadData() {


        binding.apply {
            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    if (title == "接受") {
                        type = title
                    }
                    setEmptyView(createEmptyView("暂无共享设备", emptySubTitle = "请添加设备以后共享设备"))

                    setOnItemClickListener { adapter, view, position ->
                        if (title == "接受") {
                            return@setOnItemClickListener
                        }
                        DeviceHelp.deviceShareBeanContent = data[position]
                        ActivityUtils.startActivity(MySharePersonListActivity::class.java)
                    }

                }
            }


            setOnRefreshLoadMore(smartRefreshLayout)
        }


    }

    override fun loadNetData() {
        super.loadNetData()
        loadNet("")
    }

    fun loadNet(queryKey: String) {
        val map = HashMap<String, Any>()
        map["pageNum"] = currentPage
        map["pageSize"] = 10
        map["queryKey"] = queryKey
        if (title == "接受") {
            mViewModel.shareAcceptList(map)
        } else {
            mViewModel.shareList(map)
        }
    }

    override fun observe() {
        super.observe()
        mViewModel.deviceShareBean.observe(this) {

            finishRefreshAndLoadMoreNoMoreData(binding.smartRefreshLayout, currentPage, it.total)
            if (currentPage == 1) {
                mAdapter.setNewInstance(it.content.toMutableList())
            } else {
                mAdapter.addData(it.content.toMutableList())
            }
        }
    }

}